//
//  HRZHomeFunctionCell.m
//  EMarket
//
//  Created by 石头 on 2018/12/14.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZHomeFunctionCell.h"

#import "UIButton+SPRelayout.h"

@interface HRZHomeFunctionCell()

@property (nonatomic, strong) NSArray<UIButton *> *subItems;

@end



@implementation HRZHomeFunctionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        for (UIButton *itemBtn in self.subItems) {
            [self addSubview:itemBtn];
        }
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        for (UIButton *itemBtn in self.subItems) {
            [self addSubview:itemBtn];
        }
    }
    return self;
}

- (void)onItemClick:(UIButton *)sender{
    NSInteger tag = sender.tag - 1000;
    if (self.functionItemBlock) {
        self.functionItemBlock(tag);
    }
}

#pragma mark - ----  lazy  ----
- (NSArray<UIButton *> *)subItems {

    if (!_subItems) {
        NSMutableArray *items = [NSMutableArray array];
        
        CGFloat grap = 10;
        CGFloat width = (KScreenWidth - grap * 2) * 0.2 ;
        CGFloat height = kFit(80);
        
        for (int i = 0; i < 10; i++) {
            
            int row = i < 5 ? 0 : 1;
            int index = i % 5;
            
            CGRect frame = CGRectMake(grap + index * width, row * height, width, height);
            
            UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            itemBtn.frame = frame;
            itemBtn.tag = i + 1000;
            [itemBtn setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
            [itemBtn setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
            
            
            [itemBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            itemBtn.backgroundColor = RandomColor;
            [itemBtn addTarget:self action:@selector(onItemClick:) forControlEvents:UIControlEventTouchUpInside];
            [items addObject:itemBtn];
            
            
            
            [itemBtn relayoutButton:SPButtonRelayoutType_bottom];
        }
        _subItems = [NSArray arrayWithArray:items];
    }
    return _subItems;
}

@end
